package cn.ms.doctor.Service;

import cn.ms.doctor.Entity.Drugsorder;
import cn.ms.doctor.Entity.Order;
import cn.ms.doctor.Mapper.DrugsMapper;
import cn.ms.doctor.Mapper.DrugsorderMapper;
import cn.ms.doctor.Mapper.OrderMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

//配置定时任务
@Component
public class TaskScheduledService {

    @Resource
    OrderMapper orderMapper;
    @Resource
    DrugsorderMapper drugsorderMapper;
    @Resource
    DrugsMapper drugsMapper;

    @Scheduled(fixedDelay = 10000)
    @Transactional
    public synchronized void taskTimeUpdateOrderState(){

            //获取需要修改的订单号
            List<Integer> orderIds=orderMapper.getOrderIdByNoPay();
            if (orderIds.size()==0){
                return;
            }
            for (Integer orderId : orderIds) {
                //获取订单号对应的药品以及数量
                List<Drugsorder> drugsorders=drugsorderMapper.getDrugsOrderByOrderId(orderId);
                for (Drugsorder drugsorder : drugsorders) {
                    //修改库存
                    drugsorder.setDrugsCount(drugsorder.getDrugsCount()*-1);
                    drugsMapper.updateDrugsCountByDrugsId(drugsorder);
                    //删除关联表的数据
                }
                //修改订单状态
                Order order=new Order();
                order.setOrderId(orderId);
                order.setOrderState(3);
                orderMapper.updateStateByOrderId(order);
                System.out.println("执行成功");
            }

    }
}
